<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="手机号" prop="phone">
        <el-input v-model="queryParams.phone" placeholder="请输入手机号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="姓名" prop="name">
        <el-input v-model="queryParams.name" placeholder="请输入姓名" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="昵称" prop="nickname">
        <el-input v-model="queryParams.nickname" placeholder="请输入昵称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="是否实名认证" prop="isAuthenticated">
        <el-select v-model="queryParams.isAuthenticated" placeholder="请选择是否实名认证" clearable size="small">
          <el-option v-for="dict in dict.type.didi_is_authenticated" :label="dict.label" :value="dict.value" :key="dict.value"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="余额" prop="balance">
        <el-input v-model="queryParams.balance" placeholder="请输入余额" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['user:user:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['user:user:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['user:user:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['user:user:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="userList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="用户ID" align="center" prop="id" width="80"/>
      <el-table-column label="手机号" align="center" prop="phone" />
      <el-table-column label="姓名" align="center" prop="name">
        <template slot-scope="scope">
          <el-button type="text" @click="handleUserOrder(scope.row)">
            {{ scope.row.name }}
          </el-button>
        </template>
      </el-table-column>
      <el-table-column label="昵称" align="center" prop="nickname" />
      <el-table-column label="头像url" align="center" prop="avatar" />
      <el-table-column label="是否实名认证" align="center" prop="isAuthenticated" width="150">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.didi_is_authenticated" :value="scope.row.isAuthenticated"/>
        </template>
      </el-table-column>
      <el-table-column label="禁用状态" align="center" prop="status" width="80">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.didi_user_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="余额" align="center" prop="balance" width="80" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['user:user:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['user:user:remove']">删除</el-button>
            <el-button size="mini" type="text" :icon="scope.row.status === 0 ? 'el-icon-success' : 'el-icon-error'"
            @click="handleUpdateStatus(scope.row)" v-hasPermi="['user:user:edit']">
            {{ scope.row.status === 0 ? '启用' : '禁用' }}
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改用户对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="手机号" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入手机号" />
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="form.password" placeholder="请输入密码" type="password"/>
        </el-form-item>
        <el-form-item label="姓名" prop="name">
          <el-input v-model="form.name" placeholder="请输入姓名" />
        </el-form-item>
        <el-form-item label="昵称" prop="nickname">
          <el-input v-model="form.nickname" placeholder="请输入昵称" />
        </el-form-item>
        <el-form-item label="头像url" prop="avatar">
          <el-input v-model="form.avatar" placeholder="请输入头像url" />
        </el-form-item>
        <el-form-item label="余额" prop="balance">
          <el-input v-model="form.balance" placeholder="请输入余额" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 用户订单对话框 -->
    <el-dialog title="用户订单" :visible.sync="openUserOrder" width="70vw" append-to-body>
      <el-table :data="userOrderList" style="width: 100%" >
        <el-table-column label="订单id" align="center" prop="id" width="80" />
        <el-table-column label="车辆id" align="center" prop="carId" width="80" />
        <el-table-column label="起始地点" align="center" prop="startPoint" width="width" />
        <el-table-column label="目的地" align="center" prop="endPoint" width="width" />
        <el-table-column label="订单开始时间" align="center" prop="startTime" width="width" />
        <el-table-column label="订单结束时间" align="center" prop="endTime" width="width" />
        <el-table-column label="实际支付金额(元)" align="center" prop="actualPayment" width="width" />
      </el-table>
    </el-dialog>
  </div>
</template>

<script>
import { listUser, getUser, delUser, addUser, updateUser, getUserOrderList } from "@/api/didi/user";

export default {
  name: "didiUser",
  dicts: ['didi_is_authenticated', 'didi_user_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 用户表格数据
      userList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 用户订单
      userOrderList: [],
      // 用户订单弹出层
      openUserOrder: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        phone: null,
        password: null,
        name: null,
        nickname: null,
        avatar: null,
        isAuthenticated: null,
        status: null,
        balance: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        phone: [
          { required: true, message: "手机号不能为空", trigger: "blur" }
        ],
        password: [
          { required: true, message: "密码不能为空", trigger: "blur" }
        ],
        name: [
          { required: true, message: "姓名不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询用户列表 */
    getList() {
      this.loading = true;
      listUser(this.queryParams).then(response => {
        this.userList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        phone: null,
        password: null,
        name: null,
        nickname: null,
        avatar: null,
        isAuthenticated: null,
        createTime: null,
        updateTime: null,
        status: null,
        balance: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加用户";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getUser(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改用户";
      });
    },
    /** 获取用户订单操作 */
    handleUserOrder(row) {
      const id = row.id
      this.userOrderList = [];
      this.openUserOrder = true;
      this.title = "用户订单";
      getUserOrderList(id).then(response => {
        this.userOrderList = response.rows;
      });
    },
    /** 用户状态修改 */
    handleUpdateStatus(row) {
      this.reset();
      const id = row.id
      this.$modal.confirm(row.status === 1 ? `是否确认停用编号为"${id}"的用户` : `是否确认启用编号为"${id}"的用户`).then(() => {
        this.form.id = id;
        row.status === 1 ? this.form.status = 0 : this.form.status = 1;
        updateUser(this.form).then(response => {
          this.getList();
          this.$modal.msgSuccess("操作成功");
        });
      }).catch(() => { });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateUser(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addUser(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除用户编号为"' + ids + '"的数据项？').then(function () {
        return delUser(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('user/user/export', {
        ...this.queryParams
      }, `user_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
